package org.reborn.demo

import org.reborn.engine.app._
import org.reborn.engine.input._
import org.reborn.engine.input.jinput._
import org.reborn.engine.enums._
import org.reborn.engine.model.mesh._

import net.java.games.input.{Mouse => JIMouse, Component}

import com.jogamp.opengl.util._
import com.jogamp.opengl.util.awt.TextRenderer
import javax.media.opengl._
import javax.media.opengl.fixedfunc.GLLightingFunc
import javax.media.opengl.glu._
import java.awt._
import java.awt.event._

object MouseAndKeyboardInput extends GLApp {

  def main(args:Array[String]){
	
	println("java.library.path = "+System.getProperty("java.library.path"))
	  
    val options = new GLAppOptions()
    options.title = "Mouse Input Test"
//    options.fullscreen = false
    options.debugGL = true
    
	this.startApp(options)
  }
  
  import GL._
  import GL2GL3._

  var dt = 0.0f;
  
  var rotX = 0.0f;
  var rotY = 0.0f;
  var zoom = 0.0f;
  
  val text = new TextRenderer(new Font("SansSerif", Font.BOLD, 20))
  
  var meshId = -1;
  var mesh:MutableMesh = null;
  
  var fpsAvg = 0.0f;
  
  var secs = 0.0f
  
  var wire = false;
  
  override def initScene(drawable:GLAutoDrawable){
    val gl = drawable.getGL.getGL2
    import gl._
    
    import GLLightingFunc._
    
    glEnable(GL_DEPTH_TEST)
    glDepthFunc(GL_LEQUAL)
    glHint(GL2ES1.GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST)
    glClearColor(0.3f,0.3f,0.3f,1.0f)
    
    //Enable lighting
    glEnable(GL_LIGHTING)
    glShadeModel(GL_SMOOTH);
    glEnable(GL_COLOR_MATERIAL)
    glEnable(GL_LIGHT0)
    
    glLightfv(GL_LIGHT0,GL_POSITION,Array(1.0f,1.0f,1.0f,0.0f),0)
    glLightfv(GL_LIGHT0,GL_AMBIENT,Array(0.25f,0.25f,0.25f,1.0f),0)
    glLightfv(GL_LIGHT0,GL_DIFFUSE,Array(1.0f,1.0f,1.0f,1.0f),0)
    glLightfv(GL_LIGHT0,GL_SPECULAR,Array(1.0f,1.0f,1.0f,1.0f),0)
    
    glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE)
    glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,Array(1.0f,1.0f,1.0f,1.0f),0)
    glMaterialfv(GL_FRONT_AND_BACK,GL_EMISSION,Array(0.1f,0.1f,0.1f,0.0f),0)
    //glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,180.0f)
    
    mesh = MeshShapes.Sphere(72,72,0.5f)
    mesh.appendVertData(ArrayType.Color,3,mesh(ArrayType.Normal).getDataArray)

	meshId = util.meshRenderer.register(gl,mesh,UpdateFrequency.Rarely)
  }
  
  override def updateLogic(seconds:Float){
    this.dt = seconds

    val mouse = input.mouse
    val keyboard = input.keyboard
    
    fpsAvg = ((25.0f * fpsAvg) + seconds) / 26.0f
    
    if(mouse.leftButton.pressed)
    {
    	rotY += 1.0f * mouse.changeX.value
    	rotX += 1.0f * mouse.changeY.value
    }
    
    if(keyboard.keydown(KeyboardKey.Up)) rotX -= dt * 100.0f
    if(keyboard.keydown(KeyboardKey.Down)) rotX += dt * 100.0f
    if(keyboard.keydown(KeyboardKey.Right)) rotY += dt * 100.0f
    if(keyboard.keydown(KeyboardKey.Left)) rotY -= dt * 100.0f
    
    zoom += 0.1f * mouse.changeScrollWheel.value

    wire = mouse.rightButton.pressed
   
    secs+=dt
  }
  
  override def renderScene(drawable:GLAutoDrawable){
    val gl = drawable.getGL.getGL2
    import gl._
    
    import GLLightingFunc._
    
    val shininess = (31.0f * math.cos(secs).asInstanceOf[Float]) + 96.0f
    math.max(math.min(128.0f,shininess),0.0f)
    glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,shininess)
    
    glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT)
    
    if(wire)
    	glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
    else
    	glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    
    //Render the sphere
    glLoadIdentity();
    glTranslatef(0.0f,0.0f,-3.0f + zoom)
    glRotatef(rotY,0.0f,1.0f,0.0f)
    glRotatef(rotX,1.0f,0.0f,0.0f)
    glColor3f(1.0f,1.0f,1.0f)
    util.meshRenderer.render(gl,meshId)
    
    
    if(wire)
    	glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    	
    //Render the FPS text
    val mouse = input.mouse
    val keyboard = input.keyboard
    text beginRendering(drawable.getWidth, drawable.getHeight)
    text setColor(1.0f, 1.0f, 1.0f, 1.0f);
    text draw("" + 1.0f/dt,0,0);
    text draw("" + 1.0f/fpsAvg,0,16);
    text draw("Left Click " + mouse.leftButton.pressed,0,32);
    text draw("Right Click " + mouse.rightButton.pressed,0,48);
    text draw("Up " + keyboard.keydown(KeyboardKey.Up),0,64);
    text draw("Down " + keyboard.keydown(KeyboardKey.Down),0,80);
    text draw("Right " + keyboard.keydown(KeyboardKey.Right),0,96);
    text draw("Left " + keyboard.keydown(KeyboardKey.Left),0,112);
    
    text endRendering
      
    glFlush
  }
}